function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}

Page({
  onReady: function (res) {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  data: {
    src: '',
    inputValue: "",
    currentTime: 0,
    danmuList: wx.getStorageSync("arr") || []
  },
  bindInputBlur: function (e) {
    this.data.inputValue = e.detail.value
  },
  bindSendDanmu: function () {
    let text = this.data.inputValue.trim();
    if (!text) {
      wx.showToast({
        title: '请输入弹幕内容',
        icon: "none"
      })
      return;
    }
    let color = getRandomColor();
    let arr = this.data.danmuList;
    let time = this.data.currentTime
    let msg = {
      color,
      text,
      time
    }
    arr.push(msg)
    this.videoContext.sendDanmu({
      text,
      color
    })
    wx.setStorage({
      key: 'arr',
      data: arr
    })
    this.setData({
      inputValue: ''
    })
  },
  bindPlay: function () {
    this.videoContext.play()
  },
  bindPause: function () {
    this.videoContext.pause()
  },
  videoErrorCallback: function (e) {
    console.log('视频错误信息',e.detail.errMsg)
  },
  //当前进度时间
  bindtimeupdate(e) {
    this.setData({
      currentTime: parseInt(e.detail.currentTime)
    })
  }
})